/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Windows Forms Controls Class Library
 * Version 2.00
 * 
 * Graphics Tasks Class
 *      Provides a series of shared / static methods for performing certain
 * common graphical tasks.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Text;
using System.ComponentModel;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace Adaptive.Foundation.Windows.Forms
{
    /// <summary>
    /// Provides a series of shared / static methods for performing certain
    /// common graphical tasks.
    /// </summary>
    public static class GraphicsTasks
    {
        /*--------------------------------------------------------------------------------
         * Public Constants Definitions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Shading value.
        /// </summary>
        public const int GRAY_SHADING = 100;
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Shared/Static Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Shared / Static Methods / Functions
        /// <summary>
        /// Draws a rectangle whose background fades from one color to another, from left to right.
        /// </summary>
        /// <param name="g">The <see cref="Graphics"/> instance used to perform drawing tasks.</param>
        /// <param name="rect">Rectangle object specifying client area.</param>
        /// <param name="startColor">The <see cref="Color"/> to start with, on the top and left.</param>
        /// <param name="endColor">The <see cref="Color"/> to end with, on the bottom and right.</param>
        public static void DrawGradientRectangle(Graphics g, Rectangle rect, Color startColor, Color endColor)
        {
            DrawGradientRectangle(g, rect, startColor, endColor, LinearGradientMode.Horizontal);
        }
        /// <summary>
        /// Draws a rectangle whose background fades from one color to another, from left to right.
        /// </summary>
        /// <param name="g">The <see cref="Graphics"/> instance used to perform drawing tasks.</param>
        /// <param name="left">The left-most pixel co-ordinate.</param>
        /// <param name="top">The right-most pixel co-ordinate.</param>
        /// <param name="width">The width of the box, in pixels.</param>
        /// <param name="height">The height of the box, in pixels.</param>
        /// <param name="startColor">The <see cref="Color"/> to start with, on the top and left.</param>
        /// <param name="endColor">The <see cref="Color"/> to end with, on the bottom and right.</param>
        public static void DrawGradientRectangle(Graphics g, int left, int top, int width, int height, Color startColor,
            Color endColor)
        {

            DrawGradientRectangle(g, new Rectangle(left, top, width, height), startColor,
                endColor, LinearGradientMode.Horizontal);

        }
        /// <summary>
        /// Draws a rectangle whose background fades from one color to another, from left to right,
        /// using the specified graphics object.
        /// </summary>
        /// <param name="g">The <see cref="Graphics"/> object to perform the drawing.</param>
        /// <param name="rect">The rectangle defining the drawing area.</param>
        /// <param name="startColor">The <see cref="Color"/> to start with, on the top and left.</param>
        /// <param name="endColor">The <see cref="Color"/> to end with, on the bottom and right.</param>
        /// <param name="mode">The gradient mode to draw.</param>
        public static void DrawGradientRectangle(Graphics g, Rectangle rect, Color startColor, Color endColor,
            LinearGradientMode mode)
        {
            //Really cool function :) that draws a rectangle whose background fades from one color to another, from left to right.
            //Overloaded member allows user to specify graphics object and client area with rectangle object.
            //Parameters:    objRect - Rectangle object specifying client area.
            //               objGraph - A graphics object for a form, control, or print document.
            //               clrStart - The starting color for the gradient.
            //               clrEnd - The ending color for the gradient.


            LinearGradientBrush brush = null;

            if (g != null)
            {
                brush = new LinearGradientBrush(rect, startColor, endColor, mode);
                g.FillRectangle(brush, rect);
                brush.Dispose();

            }

            brush = null;
        }
        /// <summary>
        /// Draws a line from the specified start co-ordinates to the specified end co-ordinates in the specified color.
        /// </summary>
        /// <param name="g">The <see cref="Graphics"/> instance used to perform drawing tasks.</param>
        /// <param name="left">The left staring point of the line.</param>
        /// <param name="top">The top starting point of the line.</param>
        /// <param name="width">The width of the line.</param>
        /// <param name="height">The height of the line.</param>
        /// <param name="color">The color of the line.</param>
        public static void DrawLine(Graphics g, int left, int top, int width, int height, Color color)
        {
            //Draws a line from the specified start co-ordinates to the specified end co-ordinates in the specified color.
            //Parameters:    iLeft - The left staring point of the line.
            //               iTop - The top starting point of the line.
            //               iWidth - The width of the line.
            //               iHeight - The height of the line.
            //               objColor - The color of the line.
            Pen pen = null;

            if (g != null)
            {
                //Create the pen.
                pen = new Pen(color);

                //Draw the line.
                g.DrawLine(pen, left, top, left + width - 1, top + height - 1);
                pen.Dispose();
            }
            pen = null;
        }
        /// <summary>
        /// Creates a new bitmap from the original image with a shadowed effect.
        /// </summary>
        /// <param name="image">
        /// The original <see cref="Image"/> to be shadowed.
        /// </param>
        /// <returns>
        /// Returns a <see cref="Bitmap"/> with the original image super-imposed
        /// on a shadowed version of the original.
        /// </returns>
        public static Bitmap ShadowImage(Image image)
        {
            Color color;

            color = Color.FromArgb(127, GRAY_SHADING, GRAY_SHADING, GRAY_SHADING);
            return ShadowImage(image, color);

        }
        /// <summary>
        /// Creates a new bitmap from the original image with a shadowed effect.
        /// </summary>
        /// <param name="image">The original <see cref="Image"/> to be shadowed.</param>
        /// <param name="shadowColor">The <see cref="Color"/> to use for the shadow.</param>
        /// <returns>returns a <see cref="Bitmap"/> with the original image super-imposed
        /// on a shadowed version of the original.</returns>
        public static Bitmap ShadowImage(Image image, Color shadowColor)
        {
            //Creates a grayed, half-translucent "shadow" of the provided image.
            Bitmap returnValue = null;
            int x = 0;
            int y = 0;
            int xMax = 0;
            int yMax = 0;

            //Loop through all the pixels in the bitmap.  if (the alpha portion of the 
            //pixel color is set to solid (255), replace that pixel color with the 
            //new gray/translucent color.

            returnValue = new Bitmap(image);
            xMax = returnValue.Width - 1;
            yMax = returnValue.Height - 1;
            for (x = 0; x <= xMax; x++)
            {
                for (y = 0; y <= yMax; y++)
                {
                    if (returnValue.GetPixel(x, y).A == 255)
                        returnValue.SetPixel(x, y, shadowColor);

                }
            }
            return returnValue;

        }
        /// <summary>
        /// Creates a new bitmap from the original image with a shadowed effect.
        /// </summary>
        /// <param name="image">The original <see cref="Image"/> to be shadowed.</param>
        /// <param name="shadowColor">The <see cref="Color"/> to use for the shadow.</param>
        /// <param name="omitColor">A <see cref="Color"/> in the original image that is 
        /// to be omitted from the shadowing process.</param>
        /// <returns>returns a <see cref="Bitmap"/> with the original image super-imposed
        /// on a shadowed version of the original.</returns>
        public static Bitmap ShadowImage(Image image, Color shadowColor, Color omitColor)
        {
            //Creates a grayed, half-translucent "shadow" of the provided image.

            Bitmap returnValue = null;
            int x = 0;
            int y = 0;
            int xMax = 0;
            int yMax = 0;

            //Loop through all the pixels in the bitmap.  if (the alpha portion of the 
            //pixel color is set to solid (255), replace that pixel color with the 
            //new gray/translucent color.

            returnValue = new Bitmap(image);
            xMax = returnValue.Width - 1;
            yMax = returnValue.Height - 1;
            for (x = 0; x <= xMax; x++)
            {
                for (y = 0; y <= yMax; y++)
                {
                    if ((returnValue.GetPixel(x, y).A == 255) &&
                        ((returnValue.GetPixel(x, y).R != omitColor.R) ||
                        (returnValue.GetPixel(x, y).G != omitColor.G) ||
                        (returnValue.GetPixel(x, y).B != omitColor.B)))
                        returnValue.SetPixel(x, y, shadowColor);
                    else
                        returnValue.SetPixel(x, y, Color.Transparent);

                }
            }
            return returnValue;
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Shared / Static Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Private Methods / Functions
        #endregion

    }
}
